package com.hb.trans.mvp.ui.activity


import android.os.Bundle
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.basiclib.base.BaseActivity
import com.basiclib.utils.ImmersionBar
import com.hb.trans.R
import com.hb.trans.bean.NewDetail
import com.hb.trans.global.ACTIVITY_NEW_DETAIL
import com.hb.trans.mvp.contract.NewDetailContract
import com.hb.trans.mvp.presenter.NewDetailPresenter
import com.zzhoujay.richtext.RichText
import dagger.android.AndroidInjection
import kotlinx.android.synthetic.main.activity_new_detail.*
import javax.inject.Inject


@Route(path = ACTIVITY_NEW_DETAIL)
class NewDetailActivity : BaseActivity(), NewDetailContract.View {
    @Inject
    lateinit var mPresenter: NewDetailPresenter
    @JvmField
    @Autowired
    var articleId: String? = null

    override fun getContentViewResId(): Int = R.layout.activity_new_detail

    override fun onActivityInject() {
        AndroidInjection.inject(this)
    }

    override fun attachView(savedInstanceState: Bundle?) {
        ARouter.getInstance().inject(this)
        mPresenter.attachView(this)
        ImmersionBar.with(this)
            .statusBarColor(R.color.colorPrimary)
            .init()
    }

    override fun getPresenter(): NewDetailPresenter? = mPresenter

    override fun initView() {
        RichText.initCacheDir(this)
    }

    override fun initData() {
        if (articleId != null) {
            mPresenter.getNewDetail(articleId!!)
        }
    }

    override fun setListener() {
        newDetailToolbar.backClick()?.subscribe { finish() }
    }

    override fun setNewContent(new: NewDetail?) {
        new?.let {
            tvNewTitle.text = new.title
            tvNewTime.text = new.create_time
            RichText.fromHtml(new.content).into(tvNewContent)
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        ImmersionBar.with(this).destroy()
    }
}
